/**
 * Copyright (C) 2010 altuure <altuure [AT] gmail [DOT] com> http://www.altuure.com/projects/yagdao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.altuure.yagdao.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

@Entity
@Table(name = "erp_orderitem")
public class OrderItem {
    @Id
    @GeneratedValue
    protected long id;
    @Column
    public String name;
    @Column
    public double quantity;

    @Column
    public double totalPrice;

    @ManyToOne(optional = true, fetch = FetchType.LAZY)
    public Order order;

    @ManyToOne(optional = true, fetch = FetchType.LAZY)
    public Product product;

    @Override
    public boolean equals(final Object other) {
        if (!(other instanceof OrderItem))
            return false;
        OrderItem castOther = (OrderItem) other;
        return new EqualsBuilder().append(id, castOther.id).append(name, castOther.name)
                .append(quantity, castOther.quantity).append(totalPrice, castOther.totalPrice)
                .append(order, castOther.order).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(id).append(name).append(quantity).append(totalPrice).toHashCode();
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).append("id", id).append("name", name).append("quantity", quantity)
                .append("totalPrice", totalPrice).toString();
    }

}
